//
//  CMTime+Tool.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/6/15.
//

import Foundation

extension CMTime{
    
    func toTimeInterval() -> TimeInterval{
        
        return CMTimeGetSeconds(self)
    }
    
    /// 获取视频的时长
    /// - Returns: 时长
    func transformVideoTimeToString() -> String{
        
        let timeInterval = self.toTimeInterval()
        let time = roundf(Float(timeInterval))
        
        var newTime : String = "00:00"
        if time < 10{
            newTime = String.init(format: "00:0%.0f", time)
        }
        else if time < 60{
            newTime = String.init(format: "00:%.0f", time)
        }
        else{
            let min = floorf(time / 60)
            let sec = time - (min * 60)
            if sec < 10{
                newTime = String.init(format: "%.0f:0%.0f", min,sec)
            }
            else{
                newTime = String.init(format: "%.0f:%.0f", min,sec)
            }
        }
        return newTime
    }
}
